#include "mbappwig.h"    //数据监控
#include "ui_mbappwig.h"

MbappWig::MbappWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MbappWig)
{
    ui->setupUi(this);
}
// 用于接收MQTT的槽函数
void MbappWig::dataUpSlot(QByteArray Array)
{
    QString payload=Array;
    QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
    QJsonDocument doc = QJsonDocument::fromJson(payload.toLatin1(), &jsonerror); // 将JSON数据解析为QJsonDocument对象

    if(!doc.isNull() && jsonerror.error == QJsonParseError::NoError)//检查是否解析成功
    {
        if(doc.isObject())  // 如果是一个对象
        {
            QJsonObject objectroot = doc.object();  // 获取根节点
            qDebug() << "根节点：" << objectroot;
            QStringList listkeys = objectroot.keys();// 获取JSON对象的所有键
            qDebug() << "所有键：" << listkeys;

            int type = objectroot.value("type").toInt();
            if(type!=4)
            {
                QJsonArray dataArray = objectroot.value("data").toArray(); // 获取 data 字段中的数组
                // 使用常量迭代器遍历 QJsonArray
                for (int i = 0; i < dataArray.size(); ++i)
                {
                    QJsonValue value = dataArray.at(i);
                    QJsonObject itemObj = value.toObject();
                    int key = itemObj.value("key").toInt();
                    double val = itemObj.value("val").toString().toDouble(); // 由于 val 是字符串，因此需要转换为 double
                    switch (key)
                    {
                    case 106:
                    {
                        qDebug()<<"val"<<val;
                        if(val==0)
                        {
                           ui->lineEdit_1t->setText("未启用");
                           ui->lineEdit_2t->setText("未启用");
                           ui->lineEdit_3t->setText("未启用");
                           ui->lineEdit_4t->setText("未启用");
                           ui->lineEdit_5t->setText("未启用");
                           ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==1)
                        {
                            id=1;
                           ui->lineEdit_1t->setText("运行中");
                           ui->lineEdit_2t->setText("未启用");
                           ui->lineEdit_3t->setText("未启用");
                           ui->lineEdit_4t->setText("未启用");
                           ui->lineEdit_5t->setText("未启用");
                           ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==2)
                        {
                            id=2;
                            ui->lineEdit_1t->setText("未启用");
                            ui->lineEdit_2t->setText("运行中");
                            ui->lineEdit_3t->setText("未启用");
                            ui->lineEdit_4t->setText("未启用");
                            ui->lineEdit_5t->setText("未启用");
                            ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==3)
                        {
                            id=3;
                            ui->lineEdit_1t->setText("未启用");
                            ui->lineEdit_2t->setText("未启用");
                            ui->lineEdit_3t->setText("运行中");
                            ui->lineEdit_4t->setText("未启用");
                            ui->lineEdit_5t->setText("未启用");
                            ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==4)
                        {
                            id=4;
                            ui->lineEdit_1t->setText("未启用");
                            ui->lineEdit_2t->setText("未启用");
                            ui->lineEdit_3t->setText("未启用");
                            ui->lineEdit_4t->setText("运行中");
                            ui->lineEdit_5t->setText("未启用");
                            ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==5)
                        {
                            id=5;
                            ui->lineEdit_1t->setText("未启用");
                            ui->lineEdit_2t->setText("未启用");
                            ui->lineEdit_3t->setText("未启用");
                            ui->lineEdit_4t->setText("未启用");
                            ui->lineEdit_5t->setText("运行中");
                            ui->lineEdit_6t->setText("未启用");
                        }
                        if(val==6)
                        {
                            id=6;
                            ui->lineEdit_1t->setText("未启用");
                            ui->lineEdit_2t->setText("未启用");
                            ui->lineEdit_3t->setText("未启用");
                            ui->lineEdit_4t->setText("未启用");
                            ui->lineEdit_5t->setText("未启用");
                            ui->lineEdit_6t->setText("运行中");
                        }
                    }
                        break;
                    case 107:
                        if(id==1)
                        {
                            ui->lineEdit_1q->setText(QString::number(val));
                            ui->lineEdit_2q->setText("0");
                            ui->lineEdit_3q->setText("0");
                            ui->lineEdit_4q->setText("0");
                            ui->lineEdit_5q->setText("0");
                            ui->lineEdit_6q->setText("0");
                        }
                        if(id==2)
                        {
                            ui->lineEdit_1q->setText("0");
                            ui->lineEdit_2q->setText(QString::number(val));
                            ui->lineEdit_3q->setText("0");
                            ui->lineEdit_4q->setText("0");
                            ui->lineEdit_5q->setText("0");
                            ui->lineEdit_6q->setText("0");
                        }
                        if(id==3)
                        {
                            ui->lineEdit_1q->setText("0");
                            ui->lineEdit_3q->setText(QString::number(val));
                            ui->lineEdit_2q->setText("0");
                            ui->lineEdit_4q->setText("0");
                            ui->lineEdit_5q->setText("0");
                            ui->lineEdit_6q->setText("0");
                        }
                        if(id==4)
                        {
                            ui->lineEdit_1q->setText("0");
                            ui->lineEdit_4q->setText(QString::number(val));
                            ui->lineEdit_3q->setText("0");
                            ui->lineEdit_2q->setText("0");
                            ui->lineEdit_5q->setText("0");
                            ui->lineEdit_6q->setText("0");
                        }
                        if(id==5)
                        {
                            ui->lineEdit_1q->setText("0");
                            ui->lineEdit_5q->setText(QString::number(val));
                            ui->lineEdit_3q->setText("0");
                            ui->lineEdit_4q->setText("0");
                            ui->lineEdit_2q->setText("0");
                            ui->lineEdit_6q->setText("0");
                        }
                        if(id==6)
                        {
                            ui->lineEdit_1q->setText("0");
                            ui->lineEdit_6q->setText(QString::number(val));
                            ui->lineEdit_3q->setText("0");
                            ui->lineEdit_4q->setText("0");
                            ui->lineEdit_5q->setText("0");
                            ui->lineEdit_2q->setText("0");
                        }
                        break;
                    }
                }
            }
        }
    }
}

void MbappWig::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QPixmap pic(":/new/prefix1/history.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pic);
}

MbappWig::~MbappWig()
{
    delete ui;
}
